﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Sfd.Entities;
using Sfd.Repositories;
using Sfd.Repositories.SqlClient;

namespace Sfd.Services
{
    public class ProjectService
    {
        /// <summary>
        /// Repository utilisé pour la publication de projet
        /// </summary>
        /// <remarks>Par défaut, utilise la base de données SQL Azure</remarks>
        private readonly IProjectRepository _repository;

        #region ctor
        public ProjectService(bool isOnAzure) : this(new ProjectRepository(){IsOnAzure = isOnAzure}) { }

        public ProjectService(IProjectRepository repository)
        {
            _repository = repository;
        } 
        #endregion

        /// <summary>
        /// Publie un projet, ie insère un nouveau projet dans la base de données
        /// </summary>
        /// <param name="project">Le projet a publier</param>
        public void PublishProject(AzureSyncProject project)
        {
            if (project == null)
                throw new ArgumentNullException("project");

            if (IsPublished(project.Id))
                return;

            var result = _repository.PublishProject(project);

            if (result != 1)
                throw new InvalidOperationException("Erreur à la publication du projet");
        }

        /// <summary>
        /// Indique si le projet est déjà publié ou non (ie si il est enregistré dans la base SQL Azure)
        /// </summary>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public bool IsPublished(Guid projectId)
        {
            return _repository.IsPublished(projectId) == 1;
        }

        public bool UnpublishProject(Guid projectId)
        {
            if (!IsPublished(projectId))
                return false;

            _repository.RemoveAllUsersFromProject(projectId);
            return _repository.Unpublish(projectId) == 1;
        }

        public bool AddUserToProject(Guid userId, Guid projectId)
        {
            return _repository.AddUserInProject(userId, projectId) == 1;
        }

        public bool RemoveUserFromProject(Guid userId, Guid projectId)
        {
            return _repository.RemoveUserFromProject(userId, projectId) == 1;
        }

        public bool IsUserInProject(Guid userId, Guid projectId)
        {
            return _repository.IsUserInProject(userId, projectId) == 1;
        }

        public IEnumerable<AzureSyncProject> GetUserProjects(Guid userId)
        {
            return _repository.GetUserProjects(userId);
        }

        public bool IsValidContainerName(string containerName)
        {
            if (containerName.Equals("$root"))
                return true;

            return Regex.IsMatch(containerName, @"^[a-z0-9](([a-z0-9\-[^\-])){1,61}[a-z0-9]$");
        }

        public IEnumerable<AzureSyncProject> LoadAll()
        {
            return _repository.LoadAll();
        }

        public AzureSyncProject LoadById(Guid projectId)
        {
            return _repository.LoadById(projectId);
        }
    }
}
